﻿function blackContainerClick() {
    var ArticleSpanId = "Article";
    var TriggerReadArticleContainer = "container";
    var TriggerReadArticleButton = "item-image";
    var today = new Date();
    var Enable = true;
    if (today.getHours() == $("#HiddenMessageHr").val()) {
        if (today.getMinutes() == $("#HiddenMessageMin").val()) {
            if ((today.getSeconds() - $("#HiddenMessageSec").val()) <= 1) {
                Enable = false;
            }
        }
    }
    if (Enable) {
        $("#HiddenMessageHr").val(today.getHours());
        $("#HiddenMessageMin").val(today.getMinutes());
        $("#HiddenMessageSec").val(today.getSeconds());
        /*瀏覽文章事件*/
        $("." + TriggerReadArticleContainer).on("click", "." + TriggerReadArticleButton, function (event) {
            var ArticleId = this.id.substr(ArticleSpanId.length);
            ReadArticleAllevents(ArticleId, this.id);
            /*動態更改url*/
            $.cookie('c_url', window.location.pathname);
            window.history.pushState("", "", 'http://localhost/UBOX/UboxWeb/index.aspx?a_id=' + ArticleId + '&m_id=' + this.id);
            $("." + TriggerReadArticleContainer).unbind("click");
        });
        $('.black-container').fadeTo(500, 0, hideBlackContainer);
        var url = 'http://localhost' + $.cookie('c_url');
        window.history.replaceState("", "", url);
        $('body').removeClass('noscroll').scrollTop($("#ScrollTop").val());
        /*R*/
        var chat = $.connection.codingChatHub;
        $.connection.hub.start();
        chat.server.messageGroupRemoveUser("Article" + $("#HiddenArcitleId").val());
        $("#articleContent").html("");
        $("#DivArticleImg").html("");
        $("#DivUbox").html("");
        $("#DivRelatedArcitle").html("");
        $(".container").on("click", ".loveicon", function () {
            if (($.cookie("u_id") != null) && ($.cookie("u_id") != "null")) {
                AddFavoriteArticle($(this).children().attr("class"), "12")
            }
        });
    }
}
function hideBlackContainer() {
    $('.black-container').hide();
    $('body').css('overflow', 'scroll');
    // 當預覽黑幕全部消失 才顯示body的拉霸
};
function Backpage() {
    var target = $("#click_id").val();
    if (target != $("#HiddenArcitleId").val() || $("#Introduction_page").val() == 1) {
        /*R離開原本群組*/
        var chat = $.connection.codingChatHub;
        $.connection.hub.start();
        chat.server.messageGroupRemoveUser("Article" + $("#HiddenArcitleId").val());
        /*避免留言板無法點擊*/
        CKEDITOR.instances.TextMessage.focus();
        CKEDITOR.instances.TextMessage.setData();
        /*顯示我的最愛按鈕*/
        $("#BtnAddFavorite").show();
        /*現在沒有看開相文*/
        $("#Introduction_page").val(0);
        /*記住現在文章*/
        $("#HiddenArcitleId").val(target);
        /*R加入現在群組*/
        chat.server.messageGroupAddUser("Article" + target);
        /*讀文章*/
        ArticleContent(target);
        /*讀簡介*/
        SmallIntroductionArticle(target);
        /*清除所有留言*/
        //$("#MessageStart").html("");
        /*留言卷軸事件==========*/
        /*取得最大留言筆數*/
        MessgaeNum(14, $("#click_id").val());
        /*現在留言筆數(頁)*/
        $("#HiddenMessageNowNum").val(0);
        /*留言卷軸事件End==========*/
        /*顯示留言板*/
        $(".message-board-content").show();
        /*更換留言目標*/
        $(".ButtonMessgae").attr("id", "MessgaeArcitle" + target);
        $(".TextMessage").attr("id", "TextMessgaeArcitle" + target);
    }
}

$(function () {
    //游哥撰寫==============================
    // btn登入鍵click event. show預覽畫面
    $('#btnPreview').click(function () {
        $('body').css('overflow', 'hidden');
        $('.black-container').fadeTo(500, 1).show();
        // 先將body拉霸隱藏 才顯示預覽畫面(包含預覽畫面的拉霸)
    });
    // btn取消&表單外部黑色屏蔽click event
    $('.black-container').click(blackContainerClick);


    //游哥撰寫==============================
    /*隱藏檢舉刪除紐*/
    $("#report4").hide();
    $("#report3").hide();
    /*signalR*/
    var chat = $.connection.codingChatHub;
    $.connection.hub.start();
    chat.client.groupMessage = function (messageid) {
        ReadMessage(messageid, 10, 1, 1);/*這裡後面兩個參數沒用到*/
    };
    /*記憶時間擋密集留言*/
    var today = new Date();
    $("#HiddenMessageHr").val(today.getHours());
    $("#HiddenMessageMin").val(today.getMinutes());
    $("#HiddenMessageSec").val(today.getSeconds());
    /*取消所有Enter事件*/
    $(":input").keypress(function (event) {
        if (event.which == 13) {
            return false;
        }
    });
    /*介紹文章圖片放大事件*/
    $("#DivArticleImg").on("click", ".IntroductionArticleImage", function () {
        if ($(this).attr("src") != $("#imgUboxCover").attr("src")) {
            $("#imgUboxCover").attr("src", $(this).attr("src")).hide().fadeIn(500);
        }
    });
    /*刪除,檢舉留言事件確認*/
    $("#MessageStart")
        .on("click", ".remove-badmessage-board", function () {/*刪除*/
            $('#remove-badmessage-board').fadeTo(500, 1);
            $("#MessageId").val($(this).children().attr("id"));
            $("#UserActionType").val("MessageDelete");
            return false;
        })
        .on("click", ".report-badmessage-board", function () {/*檢舉*/
            $('#report-badmessage-board').fadeTo(500, 1);
            $("#TxtReport").val("");
            $("#MessageId").val($(this).children().attr("id"));
            $("#UserActionType").val("MessageReport");
            return false;
        });
    /*刪除,檢舉文章事件確認*/
    $("#report4").click(function () {/*刪除*/
        $("#UserActionType").val("ArticleDelete");
        $('#remove-badmessage-board').fadeTo(500, 1);
        return false;
    });
    $("#report3").click(function () {/*檢舉*/
        $("#UserActionType").val("ArticleReport");
        $("#TxtReport").val("");
        $('#report-badmessage-board').fadeTo(500, 1);
        return false;
    });
    $('.reader-warning-container').click(function () {
        $('.reader-warning-container').fadeTo(500, 0, function () { $(this).hide(); });
    });
    $(".reader-warning").click(function (event) {
        event.stopPropagation();
    });
    /*檢舉執行*/
    $("#BtnReportYes").click(function () {
        if (($.cookie('u_id') != null) && ($.cookie('u_id') != "null")) {
            var str = "Message";
            switch ($("#UserActionType").val()) {
                case "MessageReport":
                    UserAction(15, $("#MessageId").val().substr(str.length), $("#TxtReport").val());
                    break;
                case "ArticleReport":
                    UserAction(16, $("#HiddenArcitleId").val(), $("#TxtReport").val());
                    break;
            }
            $('.reader-warning-container').fadeTo(500, 0, function () { $(this).hide(); });
        }else {
            uboxWarning("未登入");
        }
    });
    /*檢舉刪除取消*/
    $(".BtnNo").click(function () {
        $('.reader-warning-container').fadeTo(500, 0, function () { $(this).hide(); });
    });
    /*刪除執行*/
    $("#BtnDeleteYes").click(function () {
        if (($.cookie('u_id') != null) && ($.cookie('u_id') != "null")) {
            var str = "Message";
            switch ($("#UserActionType").val()) {
                case "MessageDelete":
                    UserAction(17, $("#MessageId").val().substr(str.length), "");
                    break;
                case "ArticleDelete":
                    UserAction(18, $("#HiddenArcitleId").val(), "");
                    break;
            }
            $('.reader-warning-container').fadeTo(500, 0, function () { $(this).hide(); });
        }else {
            uboxWarning("未登入");
        }
    });
});
/*讀取產品id與Class*/
function CompareNeedData(target, Type) {
    $.post("./Handler/HandlerMainPageArcitle.ashx", {
        type: Type
        , Target: target
    },
    function (data) {
        $.each(data, function (name, value) {
            $("#P_cate").val(value[0].cid);
            $('#P_id').removeClass();
            $('#P_id').addClass(value[0].pid + '');
        });
    });
}
/*檢舉刪除*/
function UserAction(Type, target, Content) {
    $.post("./Handler/HandlerMainPageArcitle.ashx", {
        type: Type
        , Target: target
        , content: Content
    },
    function (data) {
        $.each(data, function (name, value) {
            switch (Type) {
                case 15:
                case 16:
                    if (value[0].Result == false) {
                        uboxWarning("檢舉失敗,可能原因對方已刪除或者已檢舉過");
                    } else {
                        uboxWarning("檢舉成功");
                    }
                    break;
                case 17:
                    if (value[0].Result == false) {
                        uboxWarning("封鎖失敗,可能原因已封鎖過");
                    } else {
                        uboxWarning("封鎖成功");
                    }
                    break;
                case 18:
                    if (value[0].Result == false) {
                        uboxWarning("封鎖失敗,可能原因已封鎖過");
                    } else {
                        uboxWarning("封鎖成功");
                    }
                    break;
            }
        });
    });
}
/*加入我的最愛文章記錄*/
function AddFavoriteArticle(target, Type) {
    $.post("./Handler/HandlerMainPageArcitle.ashx", {
        type: Type
    , Target: target
    },
   function (data) {
       $.each(data, function (name, value) {
           if (value[0].Result == true) {
               uboxWarning("成功加入我的最愛");
           } else {
               uboxWarning("加入失敗,可能原因之前已加入");
           }
       });
   });
}
/*讀取開箱文章資料*/
function ArticleContent(Target) {
    $.ajax({
        /*參數===========================================*/
        url: "./Handler/HandlerMainPageArcitle.ashx"
        , type: "post"
        , data: "type=1"
            + "&Target=" + Target
        , success: function (data) {
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    $("#DivArticleImg").html("");//刪除介紹文圖片
                    $("#articleTitle").html(this.title);
                    $("#articleDate").html(this.date);
                    $("#linkAuthorPage").html(this.name);
                    $("#linkAuthorPage2").html(this.name);
                    /*隱藏檢舉刪除紐*/
                    if ($.cookie('u_id') != null && $.cookie('u_id') != "null") {
                        if ($.cookie('u_id') == this.u) {
                            $("#report4").show();
                            $("#report3").hide();
                        } else {
                            $("#report4").hide();
                            $("#report3").show();
                        }
                    } else {
                        $("#report4").hide();
                        $("#report3").hide();
                    }
                    if (this.picture != null) {
                        $("#imgUboxCover").attr("src", "data:image/jpg;base64," + this.picture).hide().fadeIn(500);
                    } else {
                        $("#imgUboxCover").attr("src", "");
                    }
                    if (this.content == null) {
                        $("#articleContent").html("文章不存在");
                    } else {
                        var temptxt = this.content.replace(/\+/g, " ");
                        var Txt = decodeURIComponent(temptxt);
                        $("#articleContent").hide().html(Txt).fadeIn(500);
                    }
                    /*回傳===========================================*/
                });
            });/*successEnd===========================================*/
        }
    });
}
/*相關文章選項*/
function RelatedArticle(Target, PageNum) {
    var i = 1;
    $.ajax({
        url: "./Handler/HandlerMainPageArcitle.ashx"
        , type: "post"
        , data: "type=2"
            + "&Target=" + Target
            + "&PageNum=" + PageNum
        , success: function (data) {
            var content = "";
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    content = "<div class='DivRelatedArcitleData' id='" + this.a + "'>";
                    content += "<div class='RelatedArcitleImg'><img src='data:image/jpg;base64," + this.picture + "'/></div>";
                    content += "<div class='RelatedArcitleContent'>" + this.title + "</div>";
                    content += "</div>";
                    $("#DivRelatedArcitle").append(content).hide().delay(i * 100).fadeIn(500);
                    i++;
                    /*回傳===========================================*/
                });
            });
            /*successEnd===========================================*/
        }
    });
}
/*分頁*/
function UnBoxingArticalNum(Target, Currentlypage) {/*取資料筆數*/
    $.ajax({
        url: "./Handler/HandlerMainPageArcitle.ashx"
        , type: "post"
        , data: "type=3"
        + "&Target=" + Target
        , success: function (data) {
            $('#pagination').html("");
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    var Currentlypage2 = parseInt(Currentlypage.substr(7));//Currentlypage目前所在頁數,避免傳字串parseInt()
                    var rowsPage = 10;  // 每頁有幾列
                    var totalRows = this.num; // 取得表格共幾列
                    var ItemNum = 5;//一定要奇數,最小3
                    var numPages = Math.floor(totalRows / rowsPage);// 計算頁數
                    var mod = totalRows % rowsPage; // 餘數
                    var EndItem;
                    var StartItem;
                    if (mod > 0) { numPages++; } // 不能整除
                    if (Currentlypage2 == 0) { Currentlypage2 = numPages; }//最後一頁處理
                    if (Currentlypage2 == -1) { Currentlypage2 = 1; }//第一頁處理
                    if (Currentlypage2 - (ItemNum - 1) / 2 > 0) {
                         StartItem = Currentlypage2 - (ItemNum - 1) / 2;
                    } else {
                         StartItem = 1;
                    }
                    if ((Currentlypage2 + (ItemNum - 1) / 2 <= numPages) && ((Currentlypage2 + (ItemNum - 1) / 2) > 0)) {
                         EndItem = Currentlypage2 + (ItemNum - 1) / 2;
                    } else {
                        EndItem = numPages;
                    }
                    $('#pagination').append('<li id=navItem-' + 1 + '>' + '<a>' + '首頁' + '</a>' + '</li>').fadeIn(500);
                    for (var i = StartItem; i <= EndItem; i++) {
                        $('#pagination').append('<li id=navItem' + i + '>' + '<a>' + i + '</a>' + '</li>').fadeIn(500);
                    }
                    $('#pagination').append('<li id=navItem' + 0 + '>' + '<a>' + '最後一頁' + '</a>' + '</li> ').fadeIn(500);
                    $("#navItem" + Currentlypage2).css('color', 'red');
                    // 按下頁碼超連結
                    $('#pagination li').click(function () {
                        var pagenum = this.id.substr(7);
                        if (pagenum == 0) { pagenum = numPages; }//最後一頁處理
                        if (pagenum == -1) { pagenum = 1; }//第一頁處理
                        $("#DivRelatedArcitle").html("");
                        RelatedArticle(Target, pagenum);/*每個分頁唯一要改---------------*/
                        $('#pagination').html("");
                        UnBoxingArticalNum(Target, this.id);
                        return false;
                    });
                    /*回傳===========================================*/
                });
            });
            /*successEnd===========================================*/
        }
    });
}
/*介紹文簡介*/
function SmallIntroductionArticle(Target) {
    $.ajax({
        url: "./Handler/HandlerMainPageArcitle.ashx"
        , type: "post"
        , data: "type=4"
            + "&Target=" + Target
        , success: function (data) {
            $.each(data, function (name, value) {
                $.each(value, function () {
                    /*回傳===========================================*/
                    var content = "";
                    if (this.content!=null && this.content.length > 300) {
                        var temptxt = this.content.replace(/\+/g, " ");
                        content = RemoveHTML(decodeURIComponent(temptxt)).substr(0, 300) + "...";
                    } else {
                        var temptxt = this.content.replace(/\+/g, " ");
                        content = decodeURIComponent(temptxt);
                    }
                    content += "全文";
                    $("#DivUbox")
                        .hide()
                        .html("<sapn class='SpanIntroductionArticle'  id='IntroductionArticle" + this.pid + "'>" + content + "</span>")
                        .fadeIn(500);
                    ProductData(this.pid);
                    /*回傳===========================================*/
                });
            });
            /*successEnd===========================================*/
        }
    });
}
/*產品相關資訊讀取*/
function ProductData(Target) {
    $.ajax({
        url: "./Handler/HandlerMainPageArcitle.ashx"
        , type: "post"
        , data: "type=5"
            + "&Target=" + Target
        , success: function (data) {
            $.each(data, function (name, value) {
                $.each(value, function () {
                    $("#productPrice").hide().html("NT$" + this.price).fadeIn(500);
                    $("#productName").html(this.name);
                });
            });
        }
    });
}
/*新增瀏覽紀錄*/
function AddBrowseHistory(Target) {
    $.ajax({
        url: "./Handler/HandlerMainPageArcitle.ashx"
        , type: "post"
        , data: "type=8"
            + "&Target=" + Target
    });
}
/*新增留言*/
function AddMessage(Target, Content, Group) {
    $.post("./Handler/HandlerMainPageArcitle.ashx", {
        type: "9"
       , target: Target
       , content: Content
    },
   function (data) {
       /*讀取訊息資料*/
       $.each(data, function (name, value) {
           if (value[0].Result != -1) {
               var chat = $.connection.codingChatHub;
               $.connection.hub.start();
               chat.server.messageGroupSend(Group, value[0].Result);
           } else {
               alert("留言失敗,可能原因文章已封鎖");
           }
           $(".ButtonMessgae").text("送出");
       });
   });
}
/*11文章ID讀取留言,10留言ID讀取留言*/
function ReadMessage(target, Type, num, currentlypage) {
    $.post("./Handler/HandlerMainPageArcitle.ashx", {
        type: Type
        , Target: target
        , Num: num
        , Currentlypage: currentlypage
    },
    function (data) {
       var content = "";
        $.each(data, function (name, value) {
            if (Type == 11) {/*由下加留言*/
                for (var i = 0; i < value.length; i++) {
                    //==================================
                    /*讀取訊息資料*/
                    if (!$.cookie("u_id")) {
                        content += MessageBoard(0, value[i].picture, value[i].name, value[i].date, value[i].content, value[i].mid);
                    } else if (value[i].uid == $.cookie("u_id")) {
                        content += MessageBoard(1, value[i].picture, value[i].name, value[i].date, value[i].content, value[i].mid);
                    } else {
                        content += MessageBoard(2, value[i].picture, value[i].name, value[i].date, value[i].content, value[i].mid);
                    }
                    //==================================
                }
                $("#MessageStart").delay(i * 200).append(content).fadeIn(500);
            } else {/*由上加留言*/
                for (i = 0; i < value.length; i++) {
                    //==================================
                    /*讀取訊息資料*/
                    if (!$.cookie("u_id")) {
                        content += MessageBoard(0, value[i].picture, value[i].name, value[i].date, value[i].content, value[i].mid);
                    } else if (value[i].uid == $.cookie("u_id")) {
                        content += MessageBoard(1, value[i].picture, value[i].name, value[i].date, value[i].content, value[i].mid);
                    } else {
                        content += MessageBoard(2, value[i].picture, value[i].name, value[i].date, value[i].content, value[i].mid);
                    }
                    //==================================
                }
                $("#MessageStart").prepend(content);
            }
            $("#ScrollEnable").val(1);
        });
    });
}
/*留言板使用者更新*/
function ReadUser() {
    $(".msg-img").attr("src", $("#cboUserImg").attr("src"));
    $(".msg-user").html($("#cmdToolBarLogin").html());
}
/*留言板*/
function MessageBoard(type, picture, name, date, content, mid) {
    var Content = "";
    Content += "<div class='msg-container'>";
    Content += "<img class='msg-img' src='data:image/jpg;base64," + picture + "' />";
    Content += "<h5 class='msg-user'>" + name + "</h5>";
    switch (type) {
        case 1:
            Content += "<a href='#' id='report1' class='remove-badmessage-board' title='刪除'><i class='glyphicon glyphicon-remove' id=Message" + mid + "></i></a>";
            break;
        case 2:
            Content += "<a href='#' id='report2' class='report-badmessage-board' title='檢舉'><i class='glyphicon glyphicon-eye-open' id=Message" + mid + "></i>&nbsp;檢舉</a>";
            break;
    }
    Content += "<h6 class='msg-time'>" + date + "</h6>";
    var temptxt = content.replace(/\+/g, " ");
    Content += "<p class='msg-content'>" + decodeURIComponent(temptxt) + "</p>";
    Content += "<div class='msg-container'><hr /></div>";
    return Content;
}
/*取得資料筆數並建立卷軸事件*/
function MessgaeNum(type, target) {
    $.post("./Handler/HandlerMainPageArcitle.ashx", {
        type: type
        , Target: target
    },
     function (data) {
         /*讀取訊息資料*/
         $.each(data, function (name, value) {
             $("#HiddenMessageMaxNum").val(value[0].datanum);
             $(".black-container").unbind("scroll").bind("scroll", function () {
                 if (
                     $(".black-container").scrollTop() + $(window).height()
                     >= $(".black-container").height() + $(".reader-bottom").height() - 150
                     && $("#ScrollEnable").val()==1) {
                     //這邊執行ajax
                     $("#ScrollEnable").val(0);
                     $("#HiddenMessageNowNum").val(parseInt($("#HiddenMessageNowNum").val()) + 1);
                     ReadMessage(target, 11, 10, $("#HiddenMessageNowNum").val());
                     if (parseInt($("#HiddenMessageNowNum").val()) * 10 >= parseInt($("#HiddenMessageMaxNum").val())) {
                         $(".black-container").unbind("scroll");
                     }
                 }
             });
         });
     });
}
/*啟動瀏覽文章所有事件*/
function ReadArticleAllevents(ArticleId, MessageID) {
    /*關閉加入比拚*/
    if ($("#btnCompare").css("display") != "none") {
        $("#BtnClickPage").next().show();
    } else {
        $("#BtnClickPage").next().hide();
    }
    /*留言卷軸事件==========*/
    $("#ScrollTop").val($('body').scrollTop());
    $('body').addClass('noscroll');
    ///*讀取留言*/
    //ReadMessage(ArticleId, 11, 10, 1);
    /*現在留言筆數(頁)*/
    $("#HiddenMessageNowNum").val(0);
    /*避免留言板無法點擊*/
    CKEDITOR.instances.TextMessage.focus();
    CKEDITOR.instances.TextMessage.setData();
    /*留言卷軸事件End==========*/
    /*顯示我的最愛按鈕*/
    $("#BtnAddFavorite").show();
    /*現在沒有看介紹文*/
    $("#Introduction_page").val(0);
    /*原本點擊文章*/
    $("#click_id").val(ArticleId);
    /*讀取產品id與Class*/
    CompareNeedData(ArticleId, 13);
    /*開啟留言板*/
    $(".message-board-content").show();
    /*記憶現在文章ID*/
    $("#HiddenArcitleId").val(ArticleId);
    /*清除所有留言*/
    //$("#MessageStart").html("");
    /*卷軸隱藏*/
    $('body').css("overflow", "hidden");
    /*避免文章過長開啟捲軸*/
    $('.reader-top').css("overflow", "hidden");
    /*ButtonMessgae記憶Id*/
    $(".ButtonMessgae").attr("id", "Messgae" + MessageID);
    $(".TextMessage").attr("id", "TextMessgae" + MessageID);
    /*新增瀏覽紀錄*/
    if (window.location.href.indexOf("index") != -1 || window.location.href.indexOf("UserBrowseHistory") != -1 || window.location.href.indexOf("UserFavorite")) {
        if (($.cookie('u_id') != null) && ($.cookie('u_id') != "null")) {
            /*開啟留言*/
            $("#cke_TextMessage").show();
            $(".ButtonMessgae").show();
            AddBrowseHistory(ArticleId);
            ReadUser();
        } else {
            /*關閉留言*/
            $("#cke_TextMessage").hide();
            $(".ButtonMessgae").hide();
            $(".msg-img").attr("src", $("#cboUserImg").attr("src"));
            $(".msg-user").html("未登入");
        }
    } else {
        $(".message-board-content").hide();
    }
    
    /*清除相關文章選項*/
    $("#DivRelatedArcitle").html("");
    /*讀取文章資料*/
    ArticleContent(ArticleId);
    /*相關文章選項*/
    RelatedArticle(ArticleId, 1);//1是第一頁
    /*分頁初始化*/
    UnBoxingArticalNum(ArticleId, "navItem1");
    /*介紹文簡介*/
    SmallIntroductionArticle(ArticleId);
    /*清除輸入框*/
    //$(".TextMessage").val("");
    /*取得最大留言筆數*/
    MessgaeNum(14, ArticleId);
    /*介紹文簡介榜定讀取文章事件*/
    $("#DivUbox").unbind().on("click", ".SpanIntroductionArticle", function () {//$("#DivUbox")父元素不能動態產生,子可以
        if ($("#Introduction_page").val() != 1) {
            /*隱藏檢舉刪除紐*/
            $("#report4").hide();
            $("#report3").hide();
            /*R離開原本群組*/
            var chat = $.connection.codingChatHub;
            $.connection.hub.start();
            chat.server.messageGroupRemoveUser("Article" + $("#HiddenArcitleId").val());
            /*解卷軸事件*/
            $(".black-container").unbind("scroll");
            /*隱藏我的最愛按鈕*/
            $("#BtnAddFavorite").hide();
            /*記住現在正在看介紹文*/
            $("#Introduction_page").val(1);
            /*關閉留言*/
            $(".message-board-content").hide();
            //文章內容
            $.ajax({
                url: "./Handler/HandlerMainPageArcitle.ashx"
                , type: "post"
                , data: "type=6"
                    + "&Target=" + (this.id).substr(19)
                , success: function (data) {
                    $.each(data, function (name, value) {
                        $.each(value, function () {
                            /*success============================================*/
                            $("#articleTitle").html($("#productName").html() + "本站介紹");
                            $("#articleDate").html("");
                            $("#linkAuthorPage").html("本站");
                            if (this.content == null) {
                                $("#articleContent").html("文章不存在");
                            } else {
                                var temptxt = this.content.replace(/\+/g, " ");
                                var Txt = decodeURIComponent(temptxt);
                                $("#articleContent").hide().html(Txt).fadeIn(500);
                            }
                            /*===================================================*/
                        });
                    });
                    /*successEnd===========================================*/
                }
            });
            //文章圖片
            $.ajax({
                url: "./Handler/HandlerMainPageArcitle.ashx"
                , type: "post"
                , data: "type=7"
                    + "&Target=" + (this.id).substr(19)
                , success: function (data) {
                    var i = 1;
                    $("#DivArticleImg").html("");
                    $.each(data, function (name, value) {
                        $.each(value, function () {
                            /*success============================================*/
                            var node1 = document.createElement("img");
                            if (i == 1) {
                                $("#imgUboxCover").hide().fadeIn(500).attr("src", "data:image/jpg;base64," + this.picture);
                            }
                            node1.setAttribute("src", "data:image/jpg;base64," + this.picture);
                            node1.setAttribute("width", "16.6%");
                            node1.setAttribute("heigh", "95");
                            node1.setAttribute("id", "IntroductionArticleImage" + i);
                            node1.setAttribute("class", "IntroductionArticleImage");
                            $("#DivArticleImg").prepend(node1);
                            $("#IntroductionArticleImage" + i).hide();
                            $("#IntroductionArticleImage" + i).fadeIn(200 * i);
                            i++;
                            /*===================================================*/
                        });
                    });
                    /*successEnd===========================================*/
                }
            });
        }
    });
    /*相關文章選項榜定讀取文章事件*/
    $("#DivRelatedArcitle").unbind().on("click", ".DivRelatedArcitleData", function () {
        if (this.id != $("#HiddenArcitleId").val() || $("#Introduction_page").val() == 1) {
            /*R離開原本群組*/
            var chat = $.connection.codingChatHub;
            $.connection.hub.start();
            chat.server.messageGroupRemoveUser("Article" + $("#HiddenArcitleId").val());
            /*避免留言板無法點擊*/
            CKEDITOR.instances.TextMessage.focus();
            CKEDITOR.instances.TextMessage.setData();
            /*顯示我的最愛按鈕*/
            $("#BtnAddFavorite").show();
            /*現在沒有看開相文*/
            $("#Introduction_page").val(0);
            /*記住正在看的文章*/
            $("#HiddenArcitleId").val(this.id);
            /*R加入現在群組*/
            chat.server.messageGroupAddUser("Article" + $("#HiddenArcitleId").val());
            /*讀取產品id與Class*/
            CompareNeedData(this.id, 13);
            $(".message-board-content").show();
            /*清除所有留言*/
            //$("#MessageStart").html("");
            if ($.cookie('u_id')) {
                AddBrowseHistory(this.id);
                $("#HiddenArcitleId").val(this.id);
            }
            ArticleContent(this.id);//Class.DivRelatedArcitleData不能是$(this).id
            /*留言卷軸事件==========*/
            /*取得最大留言筆數*/
            MessgaeNum(14, this.id);
            /*現在留言筆數(頁)*/
            $("#HiddenMessageNowNum").val(0);
            /*留言卷軸事件End==========*/
            /*更換留言目標*/
            $(".ButtonMessgae").attr("id", "MessgaeArcitle" + this.id);
            $(".TextMessage").attr("id", "TextMessgaeArcitle" + this.id);
            SmallIntroductionArticle(this.id);
        }
    });
    /*1.回原本文章,2.加入我的最愛*/
    $(".ubox-top").unbind()
                            .on("click", "#BtnAddFavorite", function () {
                                if (($.cookie("u_id") != null) && ($.cookie("u_id") != "null")) {
                                    AddFavoriteArticle($("#HiddenArcitleId").val(), "12");
                                } else {
                                    showLogin();
                                    $('.black-container').hide();
                                }
                            });

    var width = $(window).width();
    if (width > 1000) {
        $(".ubox-top").on("click", "#BtnClickPage", Backpage);
    }
    else {
        $(".ubox-top").on("click", "#BtnClickPage", blackContainerClick);
    }


    /*R*/
        var chat = $.connection.codingChatHub;
        $.connection.hub.start();
        chat.server.messageGroupAddUser(MessageID);
    /*R*/
    var ArticleSpanId = "Article";
    /*新增留言Button事件*/
    $(".ButtonMessgae").unbind().click(function () {

        var today = new Date();
        var Enable = true;
        if (today.getHours() == $("#HiddenMessageHr").val()) {
            if (today.getMinutes() == $("#HiddenMessageMin").val()) {
                if ((today.getSeconds() - $("#HiddenMessageSec").val()) <= 2) {
                    Enable = false;
                }
            }
        }
        if (($.cookie('u_id') != null) && ($.cookie('u_id') != "null")) {
            if (CKEDITOR.instances.TextMessage.getData() != "") {
                if (Enable) {
                    var BtnId = "Messgae" + ArticleSpanId;
                    AddMessage(this.id.substr(BtnId.length), encodeURIComponent(CKEDITOR.instances.TextMessage.getData()), "Article" + $("#HiddenArcitleId").val());
                    CKEDITOR.instances.TextMessage.setData();
                } else {
                    alert("請勿密集留言");
                    CKEDITOR.instances.TextMessage.setData();
                }
            }
        } else {
            uboxWarning("請登入帳號");
        }
        //$(".TextMessage").val("");
        $("#HiddenMessageHr").val(today.getHours());
        $("#HiddenMessageMin").val(today.getMinutes());
        $("#HiddenMessageSec").val(today.getSeconds());
    });
    /*黑層顯現,捲軸到頂*/
    $('.black-container').fadeTo(500, 1).scrollTop(0);
}



function ReadArticleAllevents2(ArticleId, MessageID) {
    /*留言卷軸事件==========*/
    $("#ScrollTop").val($('body').scrollTop());
    $('body').addClass('noscroll');
    ///*取得最大留言筆數*/
    //MessgaeNum(14, ArticleId);
    /*現在留言筆數(頁)*/
    $("#HiddenMessageNowNum").val(0);
    /*留言卷軸事件End==========*/
    /*顯示我的最愛按鈕*/
    $("#BtnAddFavorite").show();
    /*現在沒有看介紹文*/
    $("#Introduction_page").val(0);
    /*原本點擊文章*/
    $("#click_id").val(ArticleId);
    /*讀取產品id與Class*/
    CompareNeedData(ArticleId, 13);
    /*開啟留言板*/
    $(".message-board-content").show();
    /*記憶現在文章ID*/
    $("#HiddenArcitleId").val(ArticleId);
    /*清除所有留言*/
    //$("#MessageStart").html("");
    /*黑層顯現,捲軸到頂*/
    $('.black-container').fadeTo(500, 1).scrollTop(0);
    /*卷軸隱藏*/
    $('body').css("overflow", "hidden");
    /*避免文章過長開啟捲軸*/
    $('.reader-top').css("overflow", "hidden");
    /*ButtonMessgae記憶Id*/
    $(".ButtonMessgae").attr("id", "Messgae" + MessageID);
    $(".TextMessage").attr("id", "TextMessgae" + MessageID);
    /*新增瀏覽紀錄*/
    if (window.location.href.indexOf("index") != -1 || window.location.href.indexOf("UserBrowseHistory") != -1 || window.location.href.indexOf("UserFavorite")) {
        if (($.cookie('u_id') != null) && ($.cookie('u_id') != "null")) {
            /*開啟留言*/
            $("#cke_TextMessage").show();
            $(".ButtonMessgae").show();
            AddBrowseHistory(ArticleId);
            ReadUser();
        } else {
            /*關閉留言*/
            $("#cke_TextMessage").hide();
            $(".ButtonMessgae").hide();
            $(".msg-img").attr("src", $("#cboUserImg").attr("src"));
            $(".msg-user").html("未登入");
        }
    } else {
        $(".message-board-content").hide();
    }

    /*清除相關文章選項*/
    $("#DivRelatedArcitle").html("");
    /*讀取文章資料*/
    ArticleContent(ArticleId);
    /*相關文章選項*/
    RelatedArticle(ArticleId, 1);//1是第一頁
    /*分頁初始化*/
    UnBoxingArticalNum(ArticleId, "navItem1");
    /*介紹文簡介*/
    SmallIntroductionArticle(ArticleId);
    /*清除輸入框*/
    //$(".TextMessage").val("");
    /*讀取留言*/
    ReadMessage(ArticleId, 11, 10, 1);
    /*介紹文簡介榜定讀取文章事件*/
    $("#DivUbox").unbind().on("click", ".SpanIntroductionArticle", function () {//$("#DivUbox")父元素不能動態產生,子可以
        if ($("#Introduction_page").val() != 1) {
            /*隱藏檢舉刪除紐*/
            $("#report4").hide();
            $("#report3").hide();
            /*解卷軸事件*/
            $(".black-container").unbind("scroll");
            /*隱藏我的最愛按鈕*/
            $("#BtnAddFavorite").hide();
            /*記住現在正在看介紹文*/
            $("#Introduction_page").val(1);
            /*關閉留言*/
            $(".message-board-content").hide();
            //文章內容
            $.ajax({
                url: "./Handler/HandlerMainPageArcitle.ashx"
                , type: "post"
                , data: "type=6"
                    + "&Target=" + (this.id).substr(19)
                , success: function (data) {
                    $.each(data, function (name, value) {
                        $.each(value, function () {
                            /*success============================================*/
                            $("#articleTitle").html($("#productName").html() + "本站介紹");
                            $("#articleDate").html("");
                            $("#linkAuthorPage").html("本站");
                            if (this.content == null) {
                                $("#articleContent").html("文章不存在");
                            } else {
                                var temptxt = this.content.replace(/\+/g, " ");
                                var Txt = decodeURIComponent(temptxt);
                                $("#articleContent").hide().html(Txt).fadeIn(500);
                            }
                            /*===================================================*/
                        });
                    });
                    /*successEnd===========================================*/
                }
            });
            //文章圖片
            $.ajax({
                url: "./Handler/HandlerMainPageArcitle.ashx"
                , type: "post"
                , data: "type=7"
                    + "&Target=" + (this.id).substr(19)
                , success: function (data) {
                    var i = 1;
                    $("#DivArticleImg").html("");
                    $.each(data, function (name, value) {
                        $.each(value, function () {
                            /*success============================================*/
                            var node1 = document.createElement("img");
                            if (i == 1) {
                                $("#imgUboxCover").hide().fadeIn(500).attr("src", "data:image/jpg;base64," + this.picture);
                            }
                            node1.setAttribute("src", "data:image/jpg;base64," + this.picture);
                            node1.setAttribute("width", "16.6%");
                            node1.setAttribute("heigh", "95");
                            node1.setAttribute("id", "IntroductionArticleImage" + i);
                            node1.setAttribute("class", "IntroductionArticleImage");
                            $("#DivArticleImg").prepend(node1);
                            $("#IntroductionArticleImage" + i).hide();
                            $("#IntroductionArticleImage" + i).fadeIn(200 * i);
                            i++;
                            /*===================================================*/
                        });
                    });
                    /*successEnd===========================================*/
                }
            });
        }
    });
    /*相關文章選項榜定讀取文章事件*/
    $("#DivRelatedArcitle").unbind().on("click", ".DivRelatedArcitleData", function () {
        if (this.id != $("#HiddenArcitleId").val() || $("#Introduction_page").val() == 1) {
            /*顯示我的最愛按鈕*/
            $("#BtnAddFavorite").show();
            /*現在沒有看開相文*/
            $("#Introduction_page").val(0);
            /*記住正在看的文章*/
            $("#HiddenArcitleId").val(this.id);
            /*讀取產品id與Class*/
            CompareNeedData(this.id, 13);
            $(".message-board-content").show();
            /*清除所有留言*/
            //$("#MessageStart").html("");
            if ($.cookie('u_id')) {
                AddBrowseHistory(this.id);
                $("#HiddenArcitleId").val(this.id);
            }
            ArticleContent(this.id);//Class.DivRelatedArcitleData不能是$(this).id
            /*留言卷軸事件==========*/
            /*取得最大留言筆數*/
            MessgaeNum(14, this.id);
            /*現在留言筆數(頁)*/
            $("#HiddenMessageNowNum").val(0);
            /*留言卷軸事件End==========*/
            /*更換留言目標*/
            $(".ButtonMessgae").attr("id", "MessgaeArcitle" + this.id);
            $(".TextMessage").attr("id", "TextMessgaeArcitle" + this.id);
            SmallIntroductionArticle(this.id);
        }
    });
    /*1.回原本文章,2.加入我的最愛*/
    $(".ubox-top").unbind()
        .on("click", "#BtnClickPage", function () {
            var target = $("#click_id").val();
            if (target != $("#HiddenArcitleId").val() || $("#Introduction_page").val() == 1) {
                /*顯示我的最愛按鈕*/
                $("#BtnAddFavorite").show();
                /*現在沒有看開相文*/
                $("#Introduction_page").val(0);
                /*記住現在文章*/
                $("#HiddenArcitleId").val(target);
                /*讀文章*/
                ArticleContent(target);
                /*讀簡介*/
                SmallIntroductionArticle(target);
                /*清除所有留言*/
                //$("#MessageStart").html("");
                /*留言卷軸事件==========*/
                /*取得最大留言筆數*/
                MessgaeNum(14, $("#click_id").val());
                /*現在留言筆數(頁)*/
                $("#HiddenMessageNowNum").val(0);
                /*留言卷軸事件End==========*/
                /*顯示留言板*/
                $(".message-board-content").show();
                /*更換留言目標*/
                $(".ButtonMessgae").attr("id", "MessgaeArcitle" + target);
                $(".TextMessage").attr("id", "TextMessgaeArcitle" + target);
            }
        })
        .on("click", "#BtnAddFavorite", function () {
            if (($.cookie("u_id") != null) && ($.cookie("u_id") != "null")) {
                AddFavoriteArticle($("#HiddenArcitleId").val(), "12");
            } else {
                showLogin();
                $('.black-container').hide();
            }
        });
    var ArticleSpanId = "Article";
    /*新增留言Button事件*/
    $(".ButtonMessgae").unbind().click(function () {

        var today = new Date();
        var Enable = true;
        if (today.getHours() == $("#HiddenMessageHr").val()) {
            if (today.getMinutes() == $("#HiddenMessageMin").val()) {
                if ((today.getSeconds() - $("#HiddenMessageSec").val()) <= 2) {
                    Enable = false;
                }
            }
        }
        if (($.cookie('u_id') != null) && ($.cookie('u_id') != "null")) {
            if (CKEDITOR.instances.TextMessage.getData() != "") {
                if (Enable) {
                    var BtnId = "Messgae" + ArticleSpanId;
                    AddMessage(this.id.substr(BtnId.length), encodeURIComponent(CKEDITOR.instances.TextMessage.getData()), "Article" + $("#HiddenArcitleId").val());
                    CKEDITOR.instances.TextMessage.setData();
                } else {
                    alert("請勿密集留言");
                    CKEDITOR.instances.TextMessage.setData();
                }
            }
        } else {
            uboxWarning("請登入帳號");
        }
        //$(".TextMessage").val("");
        $("#HiddenMessageHr").val(today.getHours());
        $("#HiddenMessageMin").val(today.getMinutes());
        $("#HiddenMessageSec").val(today.getSeconds());
    });
}
/*去除HTML*/
function RemoveHTML(strText) {

    var regEx = /<[^>]*>/g;

    return strText.replace(regEx, "");

}



$(window).resize(function () {

    if ($(this).width() > 1000) {
        $(".ubox-top").off("click", "#BtnClickPage", blackContainerClick).on("click", "#BtnClickPage", Backpage);
    }
    else {
        $(".ubox-top").off("click", "#BtnClickPage", Backpage).on("click", "#BtnClickPage", blackContainerClick);
    }
});

//============================vvFacebook分享鈕 by 陳昇鍇vv============================
$("#FBShare").click(function () {
    var SW = screen.width, SH = screen.height;
    SW = (SW - 800) / 2;
    SH = (SH - 400) / 4;
    //原網址為http://localhost/UBOX/UboxWeb/index.aspx?a_id=112&m_id=Article112
    //要抓112跟Article112
    var tempurl = encodeURIComponent(location.href);//網址轉碼要用這個，而且必須要是在網際網路上抓的到的位址（FB靠這個判斷頁面內容）
    //
    //var ArcitleId = $("#HiddenArcitleId").val();
    //var URL = "https://www.facebook.com/sharer/sharer.php?u=http%3a%2f%2flocalhost%2fUBOX%2fUboxWeb%2findex.aspx%3fa_id%3d" + ArcitleId + "%26m_id%3dArticle" + ArcitleId + "%26test%3d1";
    var URL = "https://www.facebook.com/sharer/sharer.php?u=" + tempurl + "%3ftest%3d1";
    var NewWindow = window.open(URL, "分享", "toolbar=0,status=0,location=0,height=300,width=750,left=" + SW + ",top=" + SH);
});
//============================^^Facebook分享鈕 by 陳昇鍇^^============================